home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 April / Macworld (1999-04).dmg / Shareware World / Comms & Internet / PageSpinner 2.1 / PageSpinner 2.1 68K / Examples / JavaScript / Dynamic Document Stationery < prev    next >
Text File  |  1997-10-09  |  6KB  |  212 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>Dynamic Document</TITLE>
  4.  
  5. <SCRIPT LANGUAGE="JavaScript">
  6. <!-- Beginning of JavaScript
  7. /*
  8.     Dynamic Document
  9.     Written by Jerry Aman, Optima System, Aug 2, 1996. 
  10.     Part of the PageSpinner distribution.
  11.  
  12.     Modified Aug 13, 1996
  13.  
  14.     October 9, 1997: Modified the tweak that will make the time 
  15.     to be displayed correctly in Navigator 3.0 since the time 
  16.     seem to be offset by one hour in 3.0 (but not in other versions)
  17.  
  18.     We will not be held responsible for any unwanted 
  19.     effects due to the usage of this script or any derivative.  
  20.     No warrantees for usability for any specific application are 
  21.     given or implied.
  22.  
  23.     You are free to use and modify this script,
  24.     if the credits above are given in the source code
  25. */
  26.  
  27. // getHourOfDay()
  28. // Returns: Integer containing the hour 0-24
  29.  
  30. function getHourOfDay()
  31. {       var now = new Date();
  32.         return(now.getHours());
  33. }
  34.  
  35.  
  36. // getTime()
  37. // Returns: Text containing the time in the format HH:MM
  38.  
  39. function getTime()
  40. {       
  41.     var now = new Date();
  42.     var minutes = now.getMinutes();
  43.     var divider = ":";
  44.  
  45.     if (minutes<10)
  46.         divider = ":0";
  47.  
  48.         // Hack to get it to display the time 
  49.         // correctly in version 3.0, (adjust for offset)
  50.     if (navigator.appVersion.lastIndexOf('3.') != -1 && 
  51.         navigator.appName.lastIndexOf('Netscape') != -1)
  52.             return( now.getHours()-1 + divider + minutes );
  53.  
  54.         // Other versions may work with this ?    
  55.     return( now.getHours() + divider + minutes );
  56. }
  57.  
  58.  
  59. // isMacintoshBrowser()
  60. // Returns: Boolean value, true if the browser is running under MacOS
  61.  
  62. function isMacintoshBrowser()
  63.     return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  64. }
  65.  
  66.  
  67. // sayHello ()
  68. // Inserts text dynamically in the document when it is called
  69. // Note that all text are dynamically inserted into the document
  70. // when a call to  this function are made in the BODY part of the file.
  71.  
  72. function sayHello ()
  73. {
  74.  
  75.  
  76.     document.write(    "I see that the time is <B>" + 
  77.                 getTime() + 
  78.                 "</B>, so I wish you" );
  79.  
  80.  
  81.     if(getHourOfDay()<5 || getHourOfDay()>19)
  82.         document.write('<FONT COLOR="2C396D"> a good night!</FONT>');
  83.     else 
  84.     {
  85.         if ( getHourOfDay() <11) 
  86.         {
  87.             document.write('<FONT COLOR="52A553"> a good morning!</FONT>');
  88.         } 
  89.         else 
  90.         { 
  91.             document.write('<FONT COLOR="ED363C"> a good day!</FONT>');
  92.         }
  93.     }
  94. }
  95.  
  96. // End of JavaScript code  -->
  97. </SCRIPT>
  98.  
  99. </HEAD>
  100. <BODY BGCOLOR=FFFFFF TEXT=000000>
  101. <H1>JavaScript Dynamic Document</H1>
  102.  
  103. <B>This stationery page contains JavaScript that creates a dynamic page</B>
  104. <P>
  105. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher. <BR>
  106. <FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
  107. <HR>
  108. Here is an example on how JavaScript can be used to provide a dynamic page that will change its contents depending upon the local time specified in the browser's system and the OS that is used by the reader.
  109. <P>
  110.  
  111. <!-- 1) Let the reader know the browser ID used
  112.       (this is often logged by servers for keeping statistics) -->
  113.  
  114. Welcome <B><SCRIPT LANGUAGE="JavaScript">
  115. <!--
  116. document.write(navigator.userAgent)
  117. // -->
  118. </SCRIPT></B>!
  119. <P>
  120.  
  121.  
  122. <!-- 2) Tell the name of the browser used -->
  123.  
  124. <SCRIPT LANGUAGE="JavaScript">
  125. <!--
  126. document.write("Nice to see that you use ", navigator.appName," ", navigator.appVersion, ".")
  127. // -->
  128. </SCRIPT>
  129. <P>
  130.  
  131.  
  132. <!-- 3) Say something that depends upon the readers local time -->
  133. <!-- This is all done in the function sayHello -->
  134.  
  135. <SCRIPT LANGUAGE="JavaScript">
  136. <!--
  137. sayHello()
  138. // -->
  139. </SCRIPT>
  140.  
  141.  
  142. <!-- 4) Finally, say something special to the MacOS users -->
  143.  
  144. <SCRIPT LANGUAGE="JavaScript">
  145. <!--
  146.  
  147. if (isMacintoshBrowser())
  148.     document.write(
  149.     '<P><FONT COLOR="385FD1">Very, very nice to see that you are using MacOS!</FONT>'); 
  150. else
  151.     document.write(
  152.     '<P>Sad too see that you are not using a Mac to view this page.')
  153. // -->
  154. </SCRIPT>
  155.  
  156. <HR>
  157.  
  158. <P>
  159. <B>How to use:</B><BR>
  160. Four small scripts are embedded inside the text contents of this file. The scripts are executed - and inserts text - when the page is being loaded. View the source file to see how this is done. Every function is commented.
  161. <P>
  162. Replace the contents inside the script with calls to your own test functions and add text you want to have displayed inside the <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function in the script. It is recommended to create functions  for complex scripts and place these in the HEAD section, see the <TT>sayHello</TT> function for an example of this. 
  163.  
  164. <P>
  165. Make sure that all text is placed inside the <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function, otherwise it will not look good on browsers that don't support JavaScript.
  166.  
  167. <P>
  168. Edit this page or copy selected scripts to create your own dynamic page!
  169. <P>
  170. <HR>
  171. <P>
  172. <B>Example code for the first dynamic text above:</B>
  173. <PRE>Welcome <B><SCRIPT LANGUAGE="JavaScript">
  174. <!--
  175. document.write(navigator.userAgent)
  176. // -->
  177. </SCRIPT></B>!</PRE>
  178.  
  179. Note that the text "<B>Welcome !</B>" is the only text that will be displayed in browsers that don't support JavaScript.
  180. <P>
  181.  You <I>may</I> also write the script without the <FONT COLOR="555555"><!--</FONT> comment (see below), but if you do this the actual script may show up in a browser that doesn't support JavaScript. This is not recommended:
  182. <FONT COLOR="FF3366"><XMP>Welcome, <B><SCRIPT>document.write(navigator.userAgent);</SCRIPT></B>!
  183. </XMP></FONT>
  184.  
  185. <P>
  186. <HR>
  187. <P>
  188. <B>This function located in the HEAD section returns true if the browser is running on a Mac:</B><BR>
  189.  
  190. <PRE>function isMacintoshBrowser()
  191.   return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  192. }
  193. </PRE>
  194.  
  195. The function can be used inside a script like this:
  196. <PRE>if (isMacintoshBrowser())
  197.   document.write('Mac specific text...'); 
  198. else
  199.   document.write('Text for other platforms...')</PRE>
  200.  
  201. <P>
  202. <HR>
  203. <P>
  204. <B>Bug note:</B><BR>
  205. In this version of the script I have modified the getTime() so that it will make the time to be displayed correctly in Navigator 3.0 (as well as the 2.0x and 4.0x version). The time seem to be offset by one hour in the 3.0 version compared to other version. This may have something to do with daylight saving time (or maybe not...).
  206.  
  207. <P>
  208. </BODY>
  209. </HTML>
  210.